import { HttpException, HttpStatus, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { log } from "console";
import e from "express";
import { resolve } from "path";
import { Repository } from "typeorm";
import { threadId } from "worker_threads";
import { MenuEntity } from "../Entity/menu.entity";
import { MenuService } from "./menu.service";
import { RoleEntity } from "../Entity/role.entity";

@Injectable()
export class RoleService {
    constructor(
        @InjectRepository(RoleEntity)
        private readonly roleResposity: Repository<RoleEntity>,
        @InjectRepository(MenuEntity)
        private readonly menuResposity: Repository<MenuEntity>,
        private menuService: MenuService,
    ) { }

    async create(role): Promise<RoleEntity[]> {
        const u = await this.findOne(role.username);

        if (u) {
            throw new HttpException({
                message: 'Input data validation failed',
                error: 'roleName must be unique',
            },
                HttpStatus.BAD_REQUEST);
        }
        return await this.roleResposity.save(role);

    }

    async findOne(rname: string): Promise<RoleEntity> {
        let list = await this.roleResposity
            .createQueryBuilder('role')
            .leftJoinAndSelect('role.menu', 'menu')
            .getMany()

        const menuList = await this.menuService.findAll();

        let temp = list.find(item => item.rname === rname);
        
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            for (let j = 0; j < element.menu.length; j++) {
                const item = menuList.find(menu => menu.id = element.menu[j].id);
                // const item = await this.menuService.findOne(element.menu[j].id);
                element.menu[j].children = item.children;
            }
        }



        // for (let i = 0; i < list.length; i++) {
        //     const element = list[i];
        //     for (let j = 0; j < element.menu.length; j++) {
        //         const item = await this.menuService.findOne(element.menu[j].id);
        //         element.menu[j].children = item.children;
        //     }
        // }

        if (temp) {
            return temp;
        } else {
            throw new HttpException({
                message: 'Please  again input rname validation',
                error: 'This role is not  exist',
            },
                HttpStatus.BAD_REQUEST);
        }
        // console.log(list[1].rname);
    }

    async findAll(): Promise<RoleEntity[]> {
        let list = await this.roleResposity
            .createQueryBuilder('role')
            .leftJoinAndSelect('role.menu', 'menu')
            .getMany()

        const menuList = await this.menuService.findAll();

        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            for (let j = 0; j < element.menu.length; j++) {
                const item = menuList.find(menu => menu.id == element.menu[j].id);
                // const item = await this.menuService.findOne(element.menu[j].id);
                element.menu[j].children = item.children;
            }
        }

        // for (let i = 0; i < list.length; i++) {
        //     for (let j = 0; j < menuList.length; j++) {
        //         if (list[i].menu)
        //     }
        // }




        //同时多个
        // function forEach(list: any[], callback: (item: any) => void) {
        //     for (let i = 0; i < list.length; i++) {
        //         const item = list[i];
        //         callback(item);
        //     }
        // }

        // forEach(list, element => {
        //     forEach(element.menu, async item => {
        //         // item.isDeleted = false;
        //         console.log(2);
        //         item = await this.menuService.findOne(item.id);
        //         //    item.children = it.children;
        //         console.log(3);

        //         // console.log(item);

        //     })
        //     // console.log(element);
        // })

        //一个接一个
        // async function forEachAsync(list: any[], callback: (item: any) => Promise<void>) {
        //     for (let i = 0; i < list.length; i++) {
        //         const item = list[i];
        //         await callback(item);
        //     }
        // }
        // forEachAsync(list, async element => {
        //     forEachAsync(element.menu, async item => {
        //         // item.isDeleted = false;
        //         console.log(2);
        //         item = await this.menuService.findOne(item.id);
        //         //    item.children = it.children;
        //         console.log(3);

        //         // console.log(item);

        //     })
        //     // console.log(element);
        // })
        return list;
    }


    async remove(id: number): Promise<void> {
        await this.roleResposity.delete(id);
    }

    async update(role: RoleEntity): Promise<void> {
        await this.roleResposity.update(role.id, role);
    }


}